/**
 * Content layout management package
 */

var layout = {
	
	header: null,
	windowList: null,
	windowContainer: null,
	
	init: function() {
		// set page areas
		this.header = $('header');
		this.windowList = $('window_list');
		this.windowContainer = $('window_container');
		
		// set statics on resize
		window.addEvent("resize",this.setStatics.bind(this));
		this.setStatics();		

		this.menus.main.load('content/public/mainmenu.html');
		
		myos.windows.init(this.windowList,this.windowContainer,{scroller:true,sorter:true});
		
		about.snapp.begin();
	},
	
	setStatics: function() {
		
		this.windowContainer.setStyle('width',window.getSize().x-this.windowList.getSize().x);
		this.windowList.setStyle('height',window.getSize().y-this.header.getSize().y);
		this.windowContainer.setStyle('height',window.getSize().y-this.header.getSize().y);
		// do this a second time to ensure that the scrollbar doesn't throw off the width
		this.windowContainer.setStyle('width',window.getSize().x-this.windowList.getSize().x);

	},
	
	/**
	 * Test function to pop a new window
	 */
	popWindow: function() {
		win = new this.SNAppWindow().create().maximize();
		
	},
	
	SNAppWindow: new Class({
		Extends: myos.Window,
		
		options: {
			element: 'snapp_window_bg',
			elements:['snapp_window_n snapp_window_title',
					'snapp_window_nw','snapp_window_ne',
					'snapp_window_w','snapp_window_e',
					'snapp_window_sw','snapp_window_s','snapp_window_se'],

			draggable: true,
			dragHandle:'snapp_window_n',

			resizable: true,
			resizeHandle:'snapp_window_resizehandle', 

			minimizable: true,
			minClass: 'window_min_button window_button_tr1 ',
			onMinimize: myos.windows.minimize,

			maximizable: true,
			maxClass: 'window_max_button window_button_tr2 ',
			margins: {
				top: 22,
				left: 12,
				right: 12,
				bottom: 12
			},

			restoreClass: 'window_restore_button window_button_tr2 ',

			closable: true,
			closeClass: 'window_close_button window_button_tr3 ',
			onClose: null,
			
			hasOptions: true,
			optionsClass: 'window_options_button window_button_tl1 ',

			taskbarAnchor: 'snapp_anchor',
			taskbarAnchorMin: 'snapp_anchor_min',		
			taskbarAnchorFocus: 'snapp_anchor_focus',
			
			anchorOptions: {
				'closeClass': 'snapp_anchor_close',
				titleClass: 'ttClass'
			}/*,
			
			CEOpts: {
				onSuccess: function() {
					alert('setting scroller');
					new MooScroll({selector:'.wc_main'});
				}
			}
			*/
		},
			
		initialize: function(options) {
			this.setOptions(options);
			this.parent(this.options);
		},
		
		getDefaultTemplate: function() {
			rv = {
				main: null,
				actions: null
			};
			rpr = new Element('div',{'class':'wc_wrapper'});
			rv.main = new Element('div',{'class':'wc_main'}).inject(rpr);
			rv.actions = new Element('div',{'class':'wc_actions'}).inject(rpr);
			
			this.setContent(rpr);
			return rv;
		}	
	})
}

layout.menus = {
	 
	main: {
		defaultURI: 'content/public/mainmenu.html',
		
		// menu object
		menu: null,
		
		load: function(fileuri) {
			if (typeOf(fileuri) == 'null') fileuri = this.defaultURI;
			
			layout.header.set('load',{
				onSuccess: function() {
					this.menu = new MenuMatic({hideDelay:500});
				}.bind(this)
			});
			layout.header.empty();
			layout.header.load(fileuri);
		},
		
		/**
		 * Build the main menu
		 * @param functions - snapp.Object
		 */
		build: function(functions) {
			if (typeOf(functions) == null) functions = snapp.session.access.getProperty('functions');
			if (typeOf(functions) == null) {
				alert('No functions available to build menu.');
				return;
			}
			// base nav element
			pEl = new Element('ul', {'id':'nav'});
			
			// replace content
			layout.header.empty();
			layout.header.grab(pEl);
			
			// function to place the menu item
			placeItem = function(menuitem) {
				//alert('adding item "'+menuitem.getProperty('label')+'" to parent "'+menuitem.getProperty('parent'));
				pid = 'mainmenuitem-'+menuitem.getProperty('parent');
				itemEl = new Element('li',{'id':'mainmenuitem-'+menuitem.getProperty('seq')})
					.grab(
						new Element('a',{
							'href':'#',
							'onclick':menuitem.getProperty('action'),
							'text':menuitem.getProperty('label')
						}
					)
				);
				
				// add as child if parent exists
				if (pEl.contains($(pid))) {
					pContent = $(pid).getChildren('ul')[0];
					if (typeOf(pContent) == 'null') {
						pContent = new Element('ul').inject($(pid));
					}
				} else {
					pContent = pEl;
				}
				
				pContent.grab(itemEl);
			}
			
			// iterate and place each item
			functions.each(placeItem);
			
			// rebuild menu..
			this.menu = new MenuMatic({hideDelay:500});
			
		}
	}
};
